/*
Picture Clerk
*/
#define APPLINE 1
#define PICLINE 2
#define PASLINE 3
#define CASHIER 4
#define CUSTOMER 5
#include "syscall.h"

int myexp ( int count ) {
  int i, val=1;
  for (i=0; i<count; i++ ) {
    val = val * 10;
  }
  return val;
}

void itoa( char arr[], int size, int val ) {
  int i, max, dig, subval, loc;
  for (i=0; i<size; i++ ) {
    arr[i] = '\0';
  }

  for ( i=1; i<=2; i++ ) {
    if (( val / myexp(i) ) == 0 ) {
      max = i-1;
      break;
    }
  }

  subval = 0;
  loc = 0;
  for ( i=max; i>=0; i-- ) {
    dig = 48 + ((val-subval) / myexp(i));
    subval = (dig-48) * myexp(max);
    arr[loc] = dig;
    loc++;
  }

  return;
}

void concatenate(char buf[], char arr1[], int len1, char arr2[], int len2){
	
	int i;

	for(i = 0; i<len1+len2; i++){
		if(i<len1){
			buf[i] = arr1[i];
		}
	  
		else if(i - len1 < len2){
			buf[i] = arr2[i - len1];
		}
		else buf[i] = '!';
	}
	
}


void PictureClerk()
{
	char buf[256];
	char buf1[256];
	char buf2[256];
  char name[16];
  char cusname[4];
  int index;
  int cust;


  index = RegisterMe(PICLINE);
  itoa(&name[11],4,index);
  name[0] = 'P'; name[1] = 'I'; name[2] = 'C'; name[3] = '_'; 
  name[4] = 'C'; name[5] = 'L'; name[6] = 'E'; name[7] = 'R';
  name[8] = 'K'; name[9] = ' '; name[10]= '#'; name[15] = '\0';


  concatenate(buf,name,16,": Picture Clerk Process created and currently running\n", 54);
  Write(buf,16+54,ConsoleOutput);

  while(1){


	  concatenate(buf,name,16,": Ready to serve next customer, now waiting for customer\n", 57);
	  Write(buf,16+57,ConsoleOutput);


	  cust = RemoveCustomer(index, PICLINE);    
	  itoa(cusname, 4, cust);

	  
	  concatenate(buf1,name,16,": Serving customer #", 20);
	  concatenate(buf2,cusname,4,", finished taking his picture\n", 30);
	  concatenate(buf,buf1,16+20,buf2,4+30);
	  Write(buf,16+20+4+30,ConsoleOutput);

	  /*
	  Write(name,16,ConsoleOutput);
	  Write(": Serving customer #", 20, ConsoleOutput);

	  Write(cusname, 4, ConsoleOutput);
	  Write(", finished taking his picture\n", 30, ConsoleOutput);
	  */
  }
}







void main(){

  /*
    Run Application Clerk
  */

  PictureClerk();


}
